from datetime import datetime
from models import db

class Flow(db.Model):
    """流程定义表"""
    __tablename__ = 'flows'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关联的组件和连接
    components = db.relationship('FlowComponent', backref='flow', cascade='all, delete-orphan')
    connections = db.relationship('FlowConnection', backref='flow', cascade='all, delete-orphan')

class FlowComponent(db.Model):
    """流程中的组件表"""
    __tablename__ = 'flow_components'
    
    id = db.Column(db.Integer, primary_key=True)
    flow_id = db.Column(db.Integer, db.ForeignKey('flows.id'), nullable=False)
    component_id = db.Column(db.String(50), nullable=False)  # 组件在流程中的唯一标识
    type = db.Column(db.String(50), nullable=False)  # 组件类型
    name = db.Column(db.String(100), nullable=False)  # 组件名称
    config = db.Column(db.JSON)  # 组件配置
    position_x = db.Column(db.Float, nullable=False)  # 组件在画布上的x坐标
    position_y = db.Column(db.Float, nullable=False)  # 组件在画布上的y坐标

class FlowConnection(db.Model):
    """流程中的连接表"""
    __tablename__ = 'flow_connections'
    
    id = db.Column(db.Integer, primary_key=True)
    flow_id = db.Column(db.Integer, db.ForeignKey('flows.id'), nullable=False)
    source_component = db.Column(db.String(50), nullable=False)  # 源组件ID
    target_component = db.Column(db.String(50), nullable=False)  # 目标组件ID
    source_handle = db.Column(db.String(50), nullable=False)  # 源输出端口
    target_handle = db.Column(db.String(50), nullable=False)  # 目标输入端口 